package com.itheima.config;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Properties;

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource ds){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置pojo的包扫描
        factoryBean.setTypeAliasesPackage("com.itheima.pojo");
        //设置连接池
        factoryBean.setDataSource(ds);
//        //设置分页拦截
//        PageInterceptor pageInterceptor = new PageInterceptor();
//        Properties props=new Properties();
//        //设置数据库类型（方言）
//        props.setProperty("helperDialect","mysql");
//        //设置是否合理查询：当前页<0,查询第一页；当前页> 最大页，查询最后一页；
//        props.setProperty("reasonable","true");
//        pageInterceptor.setProperties(props);
//        //注册拦截器
//        factoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //可写，可不写
        msc.setSqlSessionFactoryBeanName("getSqlSessionFactoryBean");
        //设置dao层的接口扫描
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }

}
